package com.bw.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.bw.entity.User;
import org.spring.kun.common.TokenUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
public class ListController {



    @RequestMapping("/list.do")
    public String list(HttpServletRequest request,Model model){
        // 从request中 获取cookie  从cookie过滤出token  再解密token 得到当前的登录用户

//        Cookie[] cookies = request.getCookies();
//        User currentUser = null;
//        if (cookies != null){
//            for (Cookie cookie : cookies) {
//                String name = cookie.getName();
//                if (name.equals("token")){
//                    String token = cookie.getValue();
//                    String userJson = JwtUtils.parseToken(token);
//                     currentUser = JSONObject.parseObject(userJson, User.class);
//                }
//            }
//        }
        //从request中 获取cookie  从cookie过滤出token  再解密token 得到当前的登录用户
        User currentUser = TokenUtils.getLoginUserFromRequest(request, User.class);

        model.addAttribute("nickName",currentUser.getNickName());
        model.addAttribute("avatarUrl",currentUser.getAvatarUrl());
        return "list";
    }
}
